#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


int main()
{
  //close(0); 
  //close(2);
  //close(1);
  
  umask(0);
 // int fd=open("log.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
 
  //int fd=open("log.txt", O_WRONLY | O_CREAT | O_APPEND, 0666);
  int fd=open("log.txt", O_RDONLY);
  if(fd < 0)
  {
    perror("open");
    return 1;
  }
  dup2(fd, 0);//输入重定向

  char line[64];

  while(1)
  {
    printf("> ");
    if(fgets(line, sizeof(line), stdin) == NULL)
    {
      break;
    }
    printf("%s", line);
  }


  dup2(fd, 1);

  printf("open fd: %d\n", fd);// printf-> stdout
  fprintf(stdout, "open fd: %d\n", fd);// printf-> stdout
  
  const char* msg= "hello world\n";
  write(1, msg, strlen(msg));
  fflush(stdout);
  close(fd);
  return 0;
}
